<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\home\controller;
use think\Controller;
use think\Db;
class LoginController extends Controller{
    public $request = null;
    /**
     * 用户登录
     * @param string $username 用户名
     * @param string $password 密码
     */
    public function index(){
        $data['user_name'] = request()->post('username','','u_trim');   
        $data['user_pwd'] = request()->post('password','','u_trim');
        if(empty($data['user_name'])) return json (['code'=>0,'msg'=>'请输入用户名','data'=>[],'jump'=>'login'], 200);   
        if(empty($data['user_pwd'])) return json (['code'=>0,'msg'=>'请输入密码','data'=>[],'jump'=>'login'], 200);
        $db = db();    
        #判断用户是否存在且是否为店铺或绑定的有店铺 并且店铺为健身类
        $user = $db->table('fanwe_user')->where(['user_name|mobile'=>$data['user_name']])->find();
        if(empty($user)) return json ([['code'=>-1,'msg'=>'用户信息不存在','data'=>[],'jump'=>'login']]);
        if($user['user_pwd'] != md5($data['user_pwd'])) return json ([['code'=>0,'msg'=>'密码不正确','data'=>[],'jump'=>'login']]);
        if($user['is_effect'] !=1 || $user['is_delete'] != 0) return json ([['code'=>0,'msg'=>'账户被禁用或已删除','data'=>[],'jump'=>'login']]);
        $sql = 'select a.user_id,a.supplier_id,a.is_delete,a.is_main,s.name as supplier_name,s.preview,s.address,s.user_id as supplier_userid,s.type from fanwe_supplier_account as a inner join fanwe_supplier as s on s.id = a.supplier_id where a.user_id = '.$user['id'].' limit 0,1';
        $seller = $db->query($sql);
        if(empty($seller)) return json ([['code'=>0,'msg'=>'请使用商户账户或子账户登录','data'=>[],'jump'=>'login']]);
        $seller = $seller[0];
//        $seller = $db->table('fanwe_supplier_account')->field('a.user_id,a.supplier_id,a.is_delete,a.is_main,s.name as supplier_name,s.preview,s.address,s.user_id as supplier_userid,s.type')->alias('a')->join('fanwe_supplier as s ',' s.id = a.supplier_id')->where(['a.user_id'=>$user['id']])->find();
        $seller['user_name'] = $user['user_name'];   $seller['mobile'] = $user['mobile'];
        $seller['is_effect'] = $user['is_effect'];   $seller['is_delete'] = $user['is_delete'];
        $seller['uniqid'] = $user['uniqid'];    $seller['preview'] = 'http://chwl.homebank.shop'.substr($seller['preview'],1);
        $seller['password'] = $user['user_pwd'];
        session_start();
        $info['userInfo'] = $seller;
        $info['sessionId'] = session_id();
        $authKey = user_md5($seller['user_name'].$seller['password'].$info['sessionId']);
        $info['authKey'] = $authKey;
        cache('Auth_'.$authKey, null);
        cache('Auth_'.$authKey, $info,config('LOGIN_SESSION_VALID'));
        // 返回信息
        $data['authKey']		= $authKey;
        $data['sessionId']		= $info['sessionId'];
        $data['userInfo']		= $seller;
        return json(['code'=>1,'msg'=>'登录成功','data'=>$data,'jump'=>'scancode'], 200);
    }
    
    public function login(){
        session('userinfo1', ['username'=>'小米','age'=>18,'code'=>1]);
        return json(session('userinfo1'));
    }
    
    public function getsession(){
        var_dump(session('userinfo1'));
    }
    /**
     * 公共项目登录接口
     * @param 
     */
    protected function uc_login($param){
        $res = request_post(config('pub_url').'index.php/home/user/login', $param);
        $res = json_decode($res,true);
        return $res;
    }
    
    /**
     * 公共项目登录接口
     * @param 
     */
    protected function uc_register($param){
        $res = request_post(config('pub_url').'index.php/home/user/register', $param);
        $res = json_decode($res,true);
        return $res;
    }
}
